from django.db import models
from django.core.exceptions import ValidationError

# Create your models here.
"""
Django 数据库迁移命令: 
python manage.py makemigrations
python manage.py migrate
"""


# 账号管理
class Account_Admin(models.Model):
    user_name = models.CharField(max_length = 20, verbose_name = '用户名', null = True, blank = True)
    user_account = models.CharField(max_length = 20, verbose_name = '账号', null = True, blank = True)
    user_password = models.CharField(max_length = 32, verbose_name = '密码', null = True, blank = True)
    user_email = models.CharField(max_length = 50, verbose_name = '邮箱', null = True, blank = True)
    user_phone = models.CharField(max_length = 20, verbose_name = '手机号', null = True, blank = True)
    # 平台名称
    platform_name = models.CharField(max_length = 20, verbose_name = '平台名称', )
    # 平台类型
    platform_choices = (
        (1, '影音'),
        (2, '邮箱'),
        (3, '游戏'),
        (4, '购物'),
        (1000, '其它'),
    )
    platform_type = models.SmallIntegerField(verbose_name = '平台类型', null = True, blank = True,
                                             choices = platform_choices, default = 1000)
    # 平台网址
    platform_url = models.CharField(max_length = 255, verbose_name = '平台网址')
    # 平台图标
    platform_icon = models.CharField(max_length = 255, verbose_name = '平台图标', null = True, blank = True)
    # 备注
    remark = models.CharField(max_length = 240, verbose_name = '备注', null = True, blank = True,
                              default = '这是一个互联网账号')


# 账号管理 - 上传图标
class Account_Admin_Icon(models.Model):
    ico_name = models.CharField(max_length = 20, verbose_name = '图标名称')
    ico_url = models.FileField(verbose_name = '图标路径', upload_to = 'AccountAdmin/PlatformIco/')


# 云商

# 用户表
class CloudShop_User(models.Model):
    user_name = models.CharField(max_length = 20, verbose_name = '用户名', null = True, blank = True)
    user_password = models.CharField(max_length = 32, verbose_name = '密码', null = True, blank = True)
    user_email = models.CharField(verbose_name = '邮箱', max_length = 50, null = True, blank = True, unique = True)
    money = models.DecimalField(verbose_name = '我的余额', default = '0.00', max_digits = 10, decimal_places = 2)

    def clean(self):
        errors = {}
        # 检查user_email的唯一性
        if CloudShop_User.objects.filter(user_email = self.user_email).exclude(pk = self.pk).exists():
            errors['user_email'] = '该邮箱地址已被注册，请使用其他邮箱。'
        if errors:
            raise ValidationError(errors)

    def save(self, *args, **kwargs):
        self.clean()
        super().save(*args, **kwargs)


# 登录或注册_验证码临时存储
class CloudShop_User_Code(models.Model):
    user_email = models.CharField(verbose_name = '邮箱', max_length = 50, null = True, blank = True, unique = True)
    user_code = models.CharField(verbose_name = '验证码', max_length = 6, null = True, blank = True)


# 商品表
class CloudShop_Goods(models.Model):
    name = models.CharField(verbose_name = '商品名称', max_length = 200)
    goods_main_img = models.FileField(verbose_name = '商品封面', upload_to = 'CloudShop/GoodsMainImg/')
    price = models.DecimalField(max_digits = 10, decimal_places = 2, verbose_name = '商品价格')
    status_choices = (
        (1, '上架'),
        (2, '下架'),
    )
    # 商品状态
    status = models.SmallIntegerField(verbose_name = '商品状态', choices = status_choices, default = 1, null = True, blank = True)
    # 商品文件路径
    goods_file = models.FileField(verbose_name = '商品文件', upload_to = 'CloudShop/GoodsFile/', default = 'CloudShop/GoodsFile/默认文件.txt')
    user = models.ForeignKey(verbose_name = '用户', to = 'CloudShop_User', on_delete = models.CASCADE)


# 订单表
class CloudShop_Order(models.Model):
    order_id = models.CharField(verbose_name = '订单号', max_length = 50, unique = True)
    goods = models.ForeignKey(verbose_name = '商品', to = 'CloudShop_Goods', on_delete = models.CASCADE)
    user = models.ForeignKey(verbose_name = '用户', to = 'CloudShop_User', on_delete = models.CASCADE)
    buy_user = models.ForeignKey(verbose_name = '购买用户', to = 'CloudShop_User', on_delete = models.CASCADE,
                                 related_name = 'buy_user')
    order_status_choices = (
        (1, '待发货'),
        (2, '待验收'),
        (3, '验收成功'),
        (4, '已退款'),
    )
    # 订单状态
    order_status = models.SmallIntegerField(verbose_name = '订单状态', choices = order_status_choices, default = 1)
    order_price = models.DecimalField(verbose_name = '订单价格', max_digits = 10, decimal_places = 2)
    order_time = models.DateTimeField(verbose_name = '订单时间', auto_now_add = True)
